package com.miyu.common.utils.page;

import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.pojo.book.BookOperate;
import com.miyu.common.pojo.book.BookPage;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.common.utils.redis.redisUtils.util.RedisValUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * 书页jx
 * 公共解析入口
 * 解析+缓存数据
 * 不做数据持久化
 *
 * @author miyu
 * @date 2022/07/12
 */
public class BookPageJx implements PageProcessor {

    /** 爬虫配置信息设置 有条件动态配置 */
    private Site site = new Site()
            /*设置编码*/
            .setCharset("utf-8")
            /*设置抓取间隔*/
            .setSleepTime(1)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (HTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
            /*设置超时时间*/
            .setTimeOut(10000)
            /* 设置重试时间*/
            .setRetrySleepTime(300)
            /*设置重试次数*/
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return this.site;
    }
    /**
     * 解析page公共方法 不同的参数解析不同
     * @param jo 乔
     * @return {@link JSONObject}
     */
    public static JSONObject bookPageJX(BookPage jo) {
          Request request = new Request();
          request.setUrl(jo.getUrl());
        /*缓存key值*/
          request.putExtra("param",jo);
        Spider.create(new BookPageJx()).addRequest(request)
                //最后数据人缓存
                .addPipeline(new SaveCacheData())
                .thread(50)
                .run();
        return null;
    }
    /**
     * 变量值为固定配置不可改变值以及附加属性
     * @param page
     */
    @SneakyThrows
    @Override
    public void process(Page page) {
        /*参数*/
        BookPage pagePJ= page.getRequest().getExtra("param");
        String item = pagePJ.getOperate();
        switch (BookOperate.getTagByCode(item)){
            /*首页搜索*/
            case SEARCH:
                CommonBookJx.BookSearch(page);
                break;
                /*获取明细*/
            case DETAIL:
                CommonBookJx.bookDetail(page);
                break;
                /*章节列表*/
            case CHAPTER:
                CommonBookJx.bookChapter(page);
                break;
                /*阅读文本*/
            case READER:
                CommonBookJx.bookReader(page);
                break;
                /*书籍采集*/
            case COLLECT:
                CommonBookJx.collectBook(page);
                break;
                /*原始章节书籍下载*/
            case DOWNLOAD:
                CommonBookJx.downloadBook(page);
                break;
            case CHECKRULE:
                CommonBookJx.checkRule(page);
                break;
            default:
                System.out.println("无当前操作");
                break;
        }
    }
}

